package Student;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;


public class Dao {
    private String filePath="student.info";
    private static Dao instance = null;
    private static List<Student> list;

    /**
     * 获取一个实例
     * @return
     */
    public static Dao getInstance(){
        if(instance==null){
            instance = new Dao();
        }
        return instance;
    }

    /** 构造方法	 */
    public Dao(){
        list = readDataFromFile();
    }

    /** 从文件中读取数据  */
    private List<Student> readDataFromFile(){
        BufferedReader br;
        ArrayList<Student> list = new ArrayList<Student>();
        try {
            br = new BufferedReader(new FileReader(filePath));
            String line;

            while((line= br.readLine())!=null){
                String[] fields = line.split(";");
                Student stu = new Student();
                stu.setNo(Integer.parseInt(fields[0]));
                stu.setName(fields[1]);
                stu.setGender(fields[2]);
                stu.setProfession(fields[3]);
                stu.setAge((!fields[4].isEmpty())?Integer.parseInt(fields[4]) : -1);
                stu.setInterest(fields[5]);
                stu.setTelephone(fields[6]);
                stu.setQq(fields[7]);
                stu.setAddress(fields[8]);

                stu.setMath(!fields[9].isEmpty()?Integer.parseInt(fields[9]):-1);
                stu.setEnglish(!fields[10].isEmpty()?Integer.parseInt(fields[10]):-1);
                stu.setJava(!fields[11].isEmpty()?Integer.parseInt(fields[11]):-1);
                stu.setDatastruct(!fields[12].isEmpty()?Integer.parseInt(fields[12]):-1);
                stu.setDatabase(!fields[13].isEmpty()?Integer.parseInt(fields[13]):-1);
                stu.setNet(!fields[14].isEmpty()?Integer.parseInt(fields[14]):-1);
                stu.setAlgorithm(!fields[15].isEmpty()?Integer.parseInt(fields[15]):-1);
                stu.setOs((!fields[16].trim().isEmpty())?Integer.parseInt(fields[16]):-1);
//				stu.setSoftware(!fields[17].isEmpty()?Integer.parseInt(fields[17]):-1);

                list.add(stu);
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /** 写数据到文件中  */
    private void writeDataIntoFile(List<Student> list){
        BufferedWriter bw;
        try {
            bw = new BufferedWriter(new FileWriter(filePath));
            for(Student stu : list){
                String line =
                        stu.getNo()+";"+
                                stu.getName()+";"+
                                stu.getGender()+";"+
                                stu.getProfession()+";"+
                                stu.getAge()+";"+
                                stu.getInterest()+";"+
                                stu.getTelephone()+";"+
                                stu.getQq()+";"+
                                stu.getAddress()+";"+

                                stu.getMath()+";"+
                                stu.getEnglish()+";"+
                                stu.getJava()+";"+
                                stu.getDatastruct()+";"+
                                stu.getDatabase()+";"+
                                stu.getNet()+";"+
                                stu.getAlgorithm()+";"+
                                stu.getOs();
//					stu.getSoftware();

                bw.write(line);
                bw.newLine();
            }
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /** 从文件中读取数据  */
    public List<Student> getList(){
        return list;
    }

    /**
     * 取数据
     */
    public Vector<Vector> getDataByVector(){
        Vector<Vector> data = new Vector<Vector>();
        for(Student stu : list){
            Vector row = new Vector();
            row.add(stu.getNo());
            row.add(stu.getName());
            row.add(stu.getGender());
            row.add(stu.getProfession());
            row.add(stu.getSumScore());
            data.add(row);
        }
        return data;
    }

    /** 显示的列  */
    public Vector<String> getColumnNames(){
        Vector<String> columnNames = new Vector<String>();
        columnNames.add("学号");
        columnNames.add("姓名");
        columnNames.add("性别");
        columnNames.add("专业");
        columnNames.add("总成绩");
        return columnNames;
    }



    public static void main(String[] args) {
        Dao dao =Dao.getInstance();
        List<Student> list = dao.getList();
        for(Student stu :list){
            String line =
                    stu.getNo()+";"+
                            stu.getName()+";"+
                            stu.getProfession()+";"+
                            stu.getGender()+";"+
                            stu.getAge()+";"+
                            stu.getInterest()+";"+
                            stu.getTelephone()+";"+
                            stu.getQq()+";"+
                            stu.getAddress()+";"+

                            stu.getMath()+";"+
                            stu.getEnglish()+";"+
                            stu.getJava()+";"+
                            stu.getDatastruct()+";"+
                            stu.getDatabase()+";"+
                            stu.getNet()+";"+
                            stu.getAlgorithm();
            System.out.println(line);
        }

    }

    /**
     * 通过no和name查询数据。
     * @param no
     * @param name
     * @return
     */
    public Vector<Vector> queryByNoAndName(String no, String name) {
        Vector<Vector> data = getDataByVector();
        if(no==null && name==null)
            return data;
        Vector<Vector> queryData = new Vector<Vector>();
        if(no==null && name!=null){
            for(Vector<String> row : data){
                if(row.get(1).contains(name))
                    queryData.add(row);
            }
        }else if(no!=null && name==null){
            for(Vector row : data){
                if((Integer)row.get(0) == Integer.parseInt(no))
                    queryData.add(row);
            }
        }else{	// 都不为空
            for(Vector row : data){
                if((Integer)row.get(0) == Integer.parseInt(no) && ((String)row.get(1)).contains(name))
                    queryData.add(row);
            }
        }
        return queryData;
    }

    /**
     * 通过no查询数据。
     * @param no
     * @return
     */
    public Student queryByNo(int no) {
        for(Student stu : list){
            if(no == stu.getNo())
                return stu;
        }
        return null;
    }

    /**
     * 添加学生信息
     * @param no
     */
    public void addRecord(Student stu) {
        list.add(stu);
        writeDataIntoFile(list);
    }

    /**
     * 删除数据
     * @param no
     */
    public void removeRecordByNo(int no) {
        Iterator<Student> it = list.iterator();
        while(it.hasNext()){
            Student st = it.next();
            if(no == st.getNo()){
                it.remove();
                break;
            }
        }
        writeDataIntoFile(list);
    }

    /**
     * 更新学生数据
     * @param no
     */
    public void updateInfoOf(Student stu) {
        for(int i=0; i<list.size(); i++){
            Student old = list.get(i);
            if(stu.getNo() == old.getNo()){
                list.set(i, stu);
                break;
            }
        }
        writeDataIntoFile(list);
    }

    /**
     * 检验学号是否存在
     */
    public boolean noHasExist(int no) {
        if(queryByNo(no)!=null)
            return true;
        return false;
    }
}
